home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / plnk081.zip / pilot-link.0.8.1 / install-todos.c < prev    next >
C/C++ Source or Header  |  1997-08-03  |  3KB  |  151 lines

  1. /* install-todolist.c:  Pilot todo list installer
  2.  *
  3.  * Copyright 1996, Robert A. Kaplan
  4.  *
  5.  * This is free software, licensed under the GNU Public License V2.
  6.  * See the file COPYING for details.
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include "pi-source.h"
  12. #include "pi-socket.h"
  13. #include "pi-dlp.h"
  14. #include "pi-todo.h"
  15.  
  16. int main(int argc, char *argv[])
  17. {
  18.   struct pi_sockaddr addr;
  19.   int db;
  20.   int sd;
  21.   int i;
  22.   int ToDo_size;
  23.   unsigned char ToDo_buf[0xffff];
  24.   struct ToDo todo;
  25.   FILE *f;
  26.   struct PilotUser U;
  27.   int ret;
  28.   int filelen;
  29.   char note_text[] = "";
  30.   char *cPtr;
  31.   char *begPtr;
  32.   char *file_text;
  33.   int cLen;
  34.   int count;
  35.  
  36. #ifndef NOTHAPPENING
  37.   if (argc < 3) {
  38.     fprintf(stderr,"usage:%s %s file [file] ...\n",argv[0],TTYPrompt);
  39.     exit(2);
  40.   }
  41.   if (!(sd = pi_socket(PI_AF_SLP, PI_SOCK_STREAM, PI_PF_PADP))) {
  42.     perror("pi_socket");
  43.     exit(1);
  44.   }
  45.     
  46.   addr.pi_family = PI_AF_SLP;
  47.   strcpy(addr.pi_device,argv[1]);
  48.   
  49.   ret = pi_bind(sd, (struct sockaddr*)&addr, sizeof(addr));
  50.   if(ret == -1) {
  51.     perror("pi_bind");
  52.     exit(1);
  53.   }
  54.  
  55.   ret = pi_listen(sd,1);
  56.   if(ret == -1) {
  57.     perror("pi_listen");
  58.     exit(1);
  59.   }
  60.  
  61.   sd = pi_accept(sd, 0, 0);
  62.   if(sd == -1) {
  63.     perror("pi_accept");
  64.     exit(1);
  65.   }
  66.  
  67.   /* Ask the pilot who it is. */
  68.   dlp_ReadUserInfo(sd,&U);
  69.   
  70.   /* Tell user (via Pilot) that we are starting things up */
  71.   dlp_OpenConduit(sd);
  72.   
  73.   /* Open the ToDo Pad's database, store access handle in db */
  74.   if(dlp_OpenDB(sd, 0, 0x80|0x40, "ToDoDB", &db) < 0) {
  75.     puts("Unable to open ToDoDB");
  76.     dlp_AddSyncLogEntry(sd, "Unable to open ToDoDB.\n");
  77.     exit(1);
  78.   }
  79. #endif
  80.   for (i=2; i<argc; i++) {
  81.  
  82.     f = fopen(argv[i], "r");
  83.     if (f == NULL) {
  84.       perror("fopen");
  85.       exit(1);
  86.     }
  87.  
  88.     fseek(f, 0, SEEK_END);
  89.     filelen = ftell(f);
  90.     fseek(f, 0, SEEK_SET);
  91.  
  92.     file_text = (char*)malloc(filelen+1);
  93.     if (file_text == NULL) {
  94.       perror("malloc()");
  95.       exit(1);
  96.     }
  97.  
  98.     fread(file_text, filelen, 1, f);
  99.  
  100.     /*printf("file: %s\n",file_text);*/
  101.  
  102.     cPtr = file_text;
  103.     begPtr = cPtr;
  104.     cLen = 0;
  105.     count = 0;
  106.     while(count < filelen) {
  107.       count++;
  108.        /*printf("c:%c.\n",*cPtr); */
  109.       if (*cPtr == '\n') {
  110.     todo.description = begPtr;
  111.     /* replace cr with terminator */
  112.     *cPtr = '\0';
  113.  
  114.     todo.priority = 4;
  115.     todo.complete = 1;
  116.     todo.indefinite = 1;
  117.     /*now = time(0);
  118.     todo.due = *localtime(&now);*/
  119.     todo.note = note_text;
  120.     ToDo_size = pack_ToDo(&todo, ToDo_buf, sizeof(ToDo_buf));
  121. printf("desc: %s\n",todo.description); 
  122. /* printf("todobuf: %s\n",ToDo_buf);       */
  123.     dlp_WriteRecord(sd, db, 0, 0, 0, ToDo_buf, ToDo_size, 0);
  124.     cPtr++;
  125.     begPtr = cPtr;
  126.     cLen = 0;
  127.       } else {
  128.     cLen++;
  129.     cPtr++;
  130.       }
  131.     }
  132.   }
  133.  
  134.   /* Close the database */
  135.   dlp_CloseDB(sd, db);
  136.  
  137.   /* Tell the user who it is, with a different PC id. */
  138.   U.lastSyncPC = 0x00010000;
  139.   U.successfulSyncDate = time(NULL);
  140.   U.lastSyncDate = U.successfulSyncDate;
  141.   dlp_WriteUserInfo(sd,&U);
  142.  
  143.   dlp_AddSyncLogEntry(sd, "Wrote ToDo to Pilot.\n");
  144.   
  145.   /* All of the following code is now unnecessary, but harmless */
  146.   
  147.   dlp_EndOfSync(sd,0);
  148.   pi_close(sd);
  149.   exit(0);
  150. }
  151.